package br.com.ruidobrasil.util.componentes
{
	import flash.events.Event;
	
	import mx.controls.DateField;
	import mx.events.FlexEvent;
	import mx.formatters.DateFormatter;

	[Event(name="change", type="flash.events.Event")]
	public class Data extends DateField {
		
		private var formatter: DateFormatter = new DateFormatter();
		
		public function Data() {
			super();
			
			this.addEventListener(FlexEvent.CREATION_COMPLETE, init);
			this.addEventListener("change", dataChange);
		}
		
		private function init(event: FlexEvent): void {
			
			formatter.formatString = "DD/MM/YYYY";
			
			this.restrict = "0-9/";
			this.formatString = "DD/MM/YYYY";
			this.yearNavigationEnabled = true;
			this.dayNames = new Array("D", "S", "T", "Q", "Q", "S", "S");
			this.monthNames = new Array("JANEIRO", "FEVEREIRO", "MARÇO", "ABRIL", "MAIO", "JUNHO", "JULHO", "AGOSTO", "SETEMBRO",
				"OUTUBRO", "NOVEMBRO", "DEZEMBRO");
		}
		
		private function dataChange(event: Event): void {
			
			if (this.text.length == 2 || this.text.length == 5) {
				this.text += "/";
			}
			
		}
		
		protected function validHandler_change(obj:Object):void {
			this.dispatchEvent(new Event("change"));
		}
		
		public function get dataFormatada(): String {
			var dataFormatada: String = null;
			
			if (this.selectedDate != null) {
				dataFormatada = formatter.format(this.selectedDate); 
			}
			
			return dataFormatada;
		}
	}
}